package com.dhnsoft.education.controller.edu.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.dhnsoft.education.entity.edu.EduCourse;
import com.dhnsoft.education.entity.edu.chapter.ChapterVo;
import com.dhnsoft.education.entity.edu.frontVo.CourseFrontVo;
import com.dhnsoft.education.entity.edu.frontVo.CourseWebVo;
import com.dhnsoft.education.entity.edu.vo.CourseInfoVo;
import com.dhnsoft.education.entity.edu.vo.CourseListVo;
import com.dhnsoft.education.entity.order.Order;
import com.dhnsoft.education.service.edu.EduChapterService;
import com.dhnsoft.education.service.edu.EduCourseService;
import com.dhnsoft.education.service.edu.EduTeacherService;
import com.dhnsoft.education.service.order.OrderService;
import com.dhnsoft.education.utils.JwtUtils;
import com.dhnsoft.education.utils.R;
import com.dhnsoft.education.vo.CourseWebVoOrder;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @ClassName CourseFrontController
 * @Author dhn
 * @date 2022.04.10 22:48
 */
@RestController
@RequestMapping("/eduservice/courseFront")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduTeacherService teacherService;

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderService orderService;

    //条件查询带分页查询课程
    @PostMapping("getFrontCourseList/{page}/{list}")
    public R getFrontCourseList(@PathVariable Long page, @PathVariable Long list, @RequestBody(required = false) CourseFrontVo courseFrontVo){
        Page<EduCourse> coursePage = new Page<>(page,list);
        Map<String,Object> map = courseService.getCourseFrontList(coursePage,courseFrontVo);
        return R.ok().data(map);
    }


    //2.课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){

        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id，查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
        //用户信息查询
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据课程id和用户id查询当前课程是否购买
        Boolean   buyCourse = isBuyCourse(courseId, memberId);
        System.out.println("member:"+memberId+" order:"+buyCourse);
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);
    }



    //根据课程id查询课程信息
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){

        CourseInfoVo courseInfo = courseService.getCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);

        return courseWebVoOrder;
    }

    public Boolean isBuyCourse(@PathVariable String courseId,@PathVariable String memberId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",memberId);
        wrapper.eq("status",1);
        int count = orderService.count(wrapper);
        System.out.println("222323:"+memberId);
        if(count>0){
            return true;
        }
        return false;
    }

    //根据课程id增加课程浏览量
    @GetMapping("addViewCount/{id}")
    public R addViewCount(@PathVariable String id){
        EduCourse course = courseService.getById(id);
        course.setViewCount(course.getViewCount()+1);
        courseService.updateById(course);
        return R.ok();
    }

    //查询课程的方法
    @GetMapping("queryCourseList")
    public R queryCourseList(){
        List<EduCourse> courseList = courseService.list(null);
        List<CourseListVo> courseListVos = new ArrayList<>();
        for(int i = 0 ;i<courseList.size();i++){
            CourseListVo courseListVo = new CourseListVo();
            courseListVo.setValue(courseList.get(i).getTitle());
            courseListVos.add(courseListVo);
        }
        return R.ok().data("courseList",courseListVos);
    }

    //模糊查询课程
    @GetMapping("queryCourseByName/{courseName}")
    public R queryCourseByName(@PathVariable String courseName){
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.like("title",courseName);
        List<EduCourse> list = courseService.list(wrapper);
        return R.ok().data("courseList",list);


    }


}
